// pages/searchjigou/searchjigou.js


import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputnr:"",
    list:[],
    jiazaizhong: false,
  },

  getinput:function(e){
    console.log(e)
    this.setData({
      inputnr:e.detail.value
    })
  },
  bindGetUserInfo(e) {
    var that = this;
    console.log(e.detail.userInfo)
    var id = e.target.dataset.id
    console.log(id)
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      if (that.data.islogin == false) {
        var code = wx.getStorageSync("code");
        ajax('/api/index/login', {
          data: {
            js_code: code,
            nickname: e.detail.userInfo.nickName,
            avatar: e.detail.userInfo.avatarUrl
          },
          header: {
            'Accept': 'application/json', // 默认值
          },
        }, res => {
          console.log(res)
          if (res.error == 0) {
            wx.setStorageSync("uqid", res.data.uqid)
            that.setData({
              uqid: res.data.uqid,
              session_key: res.data.session_key
            });
            wx.navigateTo({
              url: '../details/details?id='+id +'&type=1',
            })

          } else {
            wx.showToast({
              title: res.message,
              icon: 'none',
              duration: 2000
            })

          }
        })
      }else{
        wx.navigateTo({
          url: '../details/details?id='+id +'&type=1',
        })
      }
    } else {
      // wx.navigateTo({
      //   url: '../details/details?id='+1 +'&type=1',
      // })
      wx.showToast({
        title: '请先授权！',
        icon: 'none',
        duration: 2000
      })

    }
  },
  searchnr(){
    this.setData({
      
      list:[]
    })
    this.getlist()
  },








 
  getlist: function () {
    var that=this
    that.setData({
      jiazaizhong: true
    })
    ajax("/api/index/mechanisms_list", {
      data: {
        lng: this.data.longitude,
        lat: this.data.latitude,
        search:this.data.inputnr,
        
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
      
      if(res.error==0){
        if(res.data){
          this.setData({
            list: res.data
          })
          wx.hideLoading();
          that.setData({
            jiazaizhong: false
          })
        }else{
          this.setData({
            list: []
          })
          wx.hideLoading();
          that.setData({
            jiazaizhong: false
          })
        }
        
        
      }else{
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var uqid = wx.getStorageSync('uqid')
    this.setData({
      uqid: uqid != '' || uqid != undefined ? uqid : ''
    })


    var latitude=wx.getStorageSync("latitude")
    var longitude= wx.getStorageSync("longitude")
    if (latitude!=undefined&&latitude!=''){
      this.setData({
        latitude: latitude,
        longitude: longitude
      })
    }else{
      this.setData({
        latitude: 0,
        longitude: 0
      })
    }

    // this.getlist()

    this.setData({
      jiazaizhong:true
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})